package com.steveproject.parttimeworktree.ui.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steveproject.parttimeworktree.App;
import com.steveproject.parttimeworktree.R;
import com.steveproject.parttimeworktree.base.BaseActivity;
import com.steveproject.parttimeworktree.base.Constant;
import com.steveproject.parttimeworktree.entity.PartBaseEntity;
import com.steveproject.parttimeworktree.net.MyCallback;
import com.steveproject.parttimeworktree.net.NoValidationTask;
import com.steveproject.parttimeworktree.utils.AppConfig;
import com.steveproject.parttimeworktree.utils.SPUtils;
import com.steveproject.parttimeworktree.utils.ScreenUtil;
import com.steveproject.parttimeworktree.utils.TitleBuilder;
import com.steveproject.parttimeworktree.widget.CenterDialog;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class PartSendCodeActivity extends BaseActivity {
    @BindView(R.id.edit_1)
    TextView edit_1;

    @BindView(R.id.edit_2)
    TextView edit_2;

    @BindView(R.id.edit_3)
    TextView edit_3;

    @BindView(R.id.edit_4)
    TextView edit_4;

    @BindView(R.id.edit)
    EditText editText;
    private String phone = "";
    private String key = "";

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setListen() {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable editable) {
                String str = editText.getText().toString();
                char[] aa = str.toCharArray();
                int length = str.length();
                switch (length) {
                    case 0:
                        edit_1.setText("");
                        edit_2.setText("");
                        edit_3.setText("");
                        edit_4.setText("");
                        edit_1.setBackgroundResource(R.drawable.code_edit_shape);
                        edit_2.setBackgroundResource(R.drawable.code_edit_shape);
                        edit_3.setBackgroundResource(R.drawable.code_edit_shape);
                        edit_4.setBackgroundResource(R.drawable.code_edit_shape);
                        break;
                    case 1:
                        edit_1.setText(aa[0] + "");
                        edit_2.setText("");
                        edit_3.setText("");
                        edit_4.setText("");
                        edit_1.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_2.setBackgroundResource(R.drawable.code_edit_shape);
                        edit_3.setBackgroundResource(R.drawable.code_edit_shape);
                        edit_4.setBackgroundResource(R.drawable.code_edit_shape);
                        break;
                    case 2:
                        edit_1.setText(aa[0] + "");
                        edit_2.setText(aa[1] + "");
                        edit_3.setText("");
                        edit_4.setText("");
                        edit_1.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_2.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_3.setBackgroundResource(R.drawable.code_edit_shape);
                        edit_4.setBackgroundResource(R.drawable.code_edit_shape);
                        break;
                    case 3:
                        edit_1.setText(aa[0] + "");
                        edit_2.setText(aa[1] + "");
                        edit_3.setText(aa[2] + "");
                        edit_4.setText("");
                        edit_1.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_2.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_3.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_4.setBackgroundResource(R.drawable.code_edit_shape);
                        break;
                    case 4:
                        edit_1.setText(aa[0] + "");
                        edit_2.setText(aa[1] + "");
                        edit_3.setText(aa[2] + "");
                        edit_4.setText(aa[3] + "");
                        edit_1.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_2.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_3.setBackgroundResource(R.drawable.code_edit_select_shape);
                        edit_4.setBackgroundResource(R.drawable.code_edit_select_shape);
                        break;
                }
            }
        });
    }

    @Override
    public void setTitleBarColor() {

    }

    @OnClick({R.id.tv_login, R.id.tv_user})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_login: //登录
                partLogin();
                break;
            case R.id.tv_user://用户协议
                startActivity(new Intent(this, PartWebCommonActivity.class)
                        .putExtra("url", AppConfig.getInstance(App.getContext()).get("user_agreement"))
                        .putExtra("title", "用户协议"));
                break;

        }
    }

    //
    //登录
    private void partLogin() {
        Dialog dialog = new CenterDialog(R.layout.loading_layout, PartSendCodeActivity.this).showLoad("正在登录...");
        NoValidationTask.getApiService().partLogin(phone, editText.getText().toString(), key).enqueue(new MyCallback<PartBaseEntity>(PartSendCodeActivity.this,dialog) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
                dialog.dismiss();
                assert response.body() != null;
                String jsonStr = JSONObject.toJSONString(response.body().getData());
                JSONObject jsonObject = JSON.parseObject(jsonStr);
                String userId = jsonObject.getString("userid");
                String token = jsonObject.getString("token");
                SPUtils.put(App.getContext(), "userId", userId);
                SPUtils.put(App.getContext(), Constant.CommonInfo.TOKEN, token);
                finish();
            }

            @Override
            protected void onFailure(Throwable t) {
                dialog.dismiss();
            }
        });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_part_send_code;
    }

    @Override
    public void initView() {
        int screenWidth = ScreenUtil.getScreenWidth(this);
        int intervalWidth = ScreenUtil.dip2px(this, 315);
        int totalInterVal = (screenWidth - intervalWidth) / 3;

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) edit_1.getLayoutParams();
        lp.rightMargin = totalInterVal;
        edit_1.setLayoutParams(lp);


        RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) edit_2.getLayoutParams();
        lp1.rightMargin = totalInterVal;
        edit_2.setLayoutParams(lp1);


        RelativeLayout.LayoutParams lp2 = (RelativeLayout.LayoutParams) edit_3.getLayoutParams();
        lp2.rightMargin = totalInterVal;
        edit_3.setLayoutParams(lp2);
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});

        new TitleBuilder(this).setTitleText("")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });

    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        key = intent.getStringExtra("codeId");
    }
}
